﻿using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Interop;
using iLynx.Common;
using iLynx.Common.Threading;
using iLynx.GameOfLife.Logic;
using iLynx.GameOfLife.ViewModels;

namespace iLynx.GameOfLife
{
    public class VMContainer
    {
        public MainWindowViewModel FirstGame { get; set; }
        public MainWindowViewModel SecondGame { get; set; }

        public void Dispose()
        {
            if (null != FirstGame)
                FirstGame.Dispose();
            if (null != SecondGame)
                SecondGame.Dispose();
        }
    }
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow
    {
        private MainWindowViewModel viewModel;

        public MainWindow()
        {
            var threadManager = new ThreadManager(RuntimeCommon.DefaultLogger);
            viewModel = new MainWindowViewModel(new GameOfLifeLogic(threadManager), new CudaLogic(threadManager));
            InitializeComponent();
            PresentationSource.AddSourceChangedHandler(this, Handler);
        }

        public MainWindowViewModel ViewModel
        {
            get { return viewModel; }
        }

        private void Handler(object sender, SourceChangedEventArgs sourceChangedEventArgs)
        {
            var hwndSource = PresentationSource.FromVisual(this) as HwndSource;
            if (hwndSource == null) return;
            var hwndTarget = hwndSource.CompositionTarget;
            // this is the new WPF API to force render mode.
            //if (hwndTarget != null) hwndTarget.RenderMode = RenderMode.SoftwareOnly;
        }

        #region Overrides of Window

        /// <summary>
        /// Raises the <see cref="E:System.Windows.Window.Closing"/> event.
        /// </summary>
        /// <param name="e">A <see cref="T:System.ComponentModel.CancelEventArgs"/> that contains the event data.</param>
        protected override void OnClosing(CancelEventArgs e)
        {
            viewModel.Dispose();
            base.OnClosing(e);
        }

        #endregion
    }
}
